// pages/mall/noticeList/index.js
import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast';
const $api = require('../../../../request/api').API;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageNo: 1,
    total: 0,
    list: [],
    beeperId: '',
    filters: {
      status: '',
      address: '',
      type: '',
      starttime: '开始时间',
      endtime: '结束时间'
    },
    minDate: new Date(2019, 10, 1).getTime(),
    maxDate: new Date().getTime(),
    timeShow: false,
    timeValue: '',
    statusName: '请选择通知状态',
    typeName: '请选择事故类型',
    actionShow: false,
    popupNum: '',
    actions: [],
    timeType: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      beeperId: options.beeperId
    });
    this.getList();
  },
  openTimeSelect(e) {
    let { type } = e.currentTarget.dataset;
    this.setData({
      timeShow: true,
      timeType: type
    });
  },
  typeFilter(val) {
    switch (val) {
      case 'fire':
        val = '火警';
        break;

      case 'gasLeak':
        val = '燃气泄漏';
        break;

      case 'lockOpen':
        val = '被盗风险';
        break;
      case 'soak':
        val = '水浸溢水';
        break;
      case 'sos':
        val = '紧急求助';
        break;
      case 'fall':
        val = '洪涝灾害';
        break;
    }
    return val;
  },
  openActionsheet(e) {
    let { type } = e.currentTarget.dataset;
    let actions = [];
    //1 状态 2类型
    if (type == 1) {
      actions = [
        {
          name: '通知中',
          value: 1
        },
        {
          name: '通知成功',
          value: 10
        },
        {
          name: '通知失败',
          value: -1
        },
        {
          name: '部分成功',
          value: 5
        }
      ];
    } else {
      actions = [
        {
          name: '火警',
          value: 'fire'
        },
        {
          name: '燃气泄漏',
          value: 'gasLeak'
        },
        {
          name: '被盗风险',
          value: 'lockOpen'
        },
        {
          name: '水浸溢水',
          value: 'soak'
        },
        {
          name: '紧急求助',
          value: 'sos'
        },
        {
          name: '洪涝灾害',
          value: 'fall'
        }
      ];
    }
    this.setData({
      popupNum: type,
      actionShow: true,
      actions
    });
  },
  getList() {
    let params = {
      pageNo: this.data.pageNo,
      pageSize: 20,
      beeperId: this.data.beeperId,
      beginTime: this.data.filters.starttime == '开始时间' ? '' : this.data.filters.starttime,
      endTime: this.data.filters.endtime == '结束时间' ? '' : this.data.filters.endtime,
      notifyState: this.data.filters.status,
      notifyType: this.data.filters.type,
      address: this.data.filters.address
    };
    $api.noticeList(params).then(res => {
      let arr = this.data.list;
      if (this.data.pageNo == 1) {
        arr = res.data.data.list;
      } else {
        arr.concat(res.data.data.list);
      }
      arr.forEach(item => {
        item.typeName = this.typeFilter(item.notifyType);
      });
      this.setData({
        total: res.data.data.total,
        list: arr
      });
    });
  },
  goSearch() {
    this.setData({
      pageNo: 1
    });
    if (
      (this.data.filters.starttime != '开始时间' && this.data.filters.endtime == '结束时间') ||
      (this.data.filters.starttime == '开始时间' && this.data.filters.endtime != '结束时间')
    ) {
      Toast.fail('请选择完整开始时间和结束时间');
      return;
    }
    this.getList();
  },
  goRepeat() {
    let filters = {
      status: '',
      address: '',
      type: '',
      starttime: '开始时间',
      endtime: '结束时间'
    };
    this.setData({
      filters,
      timeValue: '',
      statusName: '请选择通知状态',
      typeName: '请选择事故类型'
    });
  },
  gotoDetail(e) {
    let { id } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/subcontract/home/pages/noticeDetail/index?identity=1&beeperId=' + this.data.beeperId + '&notifyId=' + id
    });
  },

  confirmTime(event) {
    let time = new Date(event.detail);
    let filterTime =
      time.getFullYear() +
      '-' +
      (time.getMonth() + 1).toString().padStart(2, '0') +
      '-' +
      time.getDate().toString().padStart(2, '0');
    this.setData({
      [this.data.timeType == 'start' ? 'filters.starttime' : 'filters.endtime']: filterTime,
      timeShow: false
    });
  },
  cancelTime() {
    this.setData({
      [this.data.timeType == 'start' ? 'filters.starttime' : 'filters.endtime']:
        this.data.timeType == 'start' ? '开始时间' : '结束时间',
      timeShow: false
    });
  },
  onCancel() {
    if (this.data.popupNum == 1) {
      this.setData({
        'filters.status': '',
        statusName: '请选择通知状态',
        actionShow: false
      });
    } else {
      this.setData({
        'filters.type': '',
        typeName: '请选择事故类型',
        actionShow: false
      });
    }
  },
  onClose() {
    this.setData({
      actionShow: false
    });
  },
  onSelect(e) {
    let val = e.detail;
    if (this.data.popupNum == 1) {
      this.setData({
        'filters.status': val.value,
        statusName: val.name,
        actionShow: false
      });
    } else {
      this.setData({
        'filters.type': val.value,
        typeName: val.name,
        actionShow: false
      });
    }
  },
  onInputBlur(e) {
    let val = e.detail.value;
    this.setData({
      'filters.address': val
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      pageNo: 1,
      list: []
    });
    this.getList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.list.length < this.data.total) {
      let pageNo = ++this.data.pageNo;
      this.setData({
        pageNo: pageNo
      });
      this.getList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
});
